// @Date         2021-11-17
// @Author       Aaron Zhang
// @Description
// @FileName      both.go

package log

import (
	"zin/framework/contract"
	"io"
	"os"
	"path/filepath"
)

type providerServiceLogBoth struct {
	ProviderServiceLog
}

func NewProviderServiceLogBoth(param map[string]interface{}) (interface{}, error) {
	p := &providerServiceLogBoth{}

	p.SetFormatter(param["formatter"].(contract.Formatter))
	p.SetLevel(param["level"].(contract.LogLevel))
	p.SetCtxFielder(param["ctxFielder"].(contract.CtxFielder))

	folder := param["folder"].(string)
	file := param["file"].(string)
	logFile := filepath.Join(folder, file)

	enable := param["enable_rotate"].(string)

	var fd io.Writer
	var err error

	if enable == "enable" {
		fd, err = getRotateWriter(logFile)
	} else {
		fd, err = os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
	}

	if err != nil {
		return nil, err
	}

	p.SetOutput(fd, os.Stdout)
	return p, nil
}
